package com.dyna.controller;

import cn.hutool.core.lang.Assert;
import com.dyna.domain.DefinitionsInfo;
import com.dyna.domain.DeploymentEntity;
import com.dyna.domain.TaskSheet;
import com.dyna.domain.resp.PageResult;
import com.dyna.domain.resp.R;
import com.dyna.domain.resp.ResultCode;
import com.dyna.service.FlowableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;


/**
 * @author dyna
 */
@Api(value = "工作流程")
@RestController
@AllArgsConstructor
@RequestMapping(value = "/flow")
public class FlowableController {

    private FlowableService flowableService;

    @ApiOperation(value = "部署流程")
    @GetMapping("/deploy")
    public R<Object> deploy(@RequestParam(value = "bpmnFile",required = true) String bpmnFile, @RequestParam(value = "flowName",required = false) String flowName) {
        Assert.notBlank(bpmnFile, ResultCode.PARAM_MISS.getMessage());
        return R.success(flowableService.deploy(bpmnFile,flowName));
    }

    @ApiOperation(value = "查询部署流程")
    @GetMapping("/queryDeploys")
    public R<PageResult<DeploymentEntity>> queryDeploys(@RequestParam(value = "name",required = false) String name) {
        return R.data(flowableService.queryDeploys(name));
    }

    @ApiOperation(value = "获取流程定义信息")
    @GetMapping("/definition")
    public R<PageResult<DefinitionsInfo>> definition(@RequestParam(value = "deployId",required = false) String deployId) {
        return R.data(flowableService.definition(deployId));
    }

    @ApiOperation(value = "创建任务")
    @PostMapping("/createTask")
    public R<String> createTask(@RequestParam(value = "key",required = true) String id, @RequestBody TaskSheet taskSheet) {
        Assert.notBlank(id,ResultCode.PARAM_MISS.getMessage());
        return R.data(flowableService.createTask(taskSheet,id));
    }

    @ApiOperation(value = "查询任务")
    @GetMapping("/queryTask")
    public R<PageResult<TaskSheet>> queryTask(@RequestParam(value = "key",required = true) String id) {
        return R.data(flowableService.queryTask(id));
    }

    @ApiOperation(value = "完成任务")
    @PostMapping("/compileTask")
    public R<Object> compileTask(@RequestParam(value = "taskId",required = true) String taskId, @RequestBody HashMap<String,Object> map) {
        flowableService.compileTask(taskId,map);
        return R.success(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "查询历史任务")
    @GetMapping("/queryHistoryTask")
    public R<PageResult<TaskSheet>> queryHistoryTask(@RequestParam(value = "key",required = true) String id) {
        return R.data(flowableService.queryHistoryTask(id));
    }

    @ApiOperation(value = "删除任务")
    @DeleteMapping("/delTask")
    public R<Object> delTask(@RequestParam(value = "id", required = true) String processInstanceId,@RequestParam(value = "reason", required = true) String reason) {
        flowableService.delTask(processInstanceId,reason);
        return R.success(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "删除历史任务")
    @DeleteMapping("/delHistoryTask")
    public R<Object> delHistoryTask(@RequestParam(value = "processInstanceId", required = true) String processInstanceId) {
        flowableService.delHistoryTask(processInstanceId);
        return R.success(ResultCode.SUCCESS);
    }

}
